/**
 * Copyright 2008 Adam Ruggles.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package kiff.util.model;

import java.io.Serializable;
import java.util.Collection;
import java.util.LinkedList;

import org.apache.commons.lang.builder.ReflectionToStringBuilder;

/**
 * A Default implementation of the tree node.
 * @author Adam
 * @version $Id$
 * 
 * Created on Dec 4, 2008 at 6:44:35 PM 
 */
public class DefaultTreeNode implements TreeNode, Serializable {
	/**
	 * Serial Version UID.
	 */
	private static final long serialVersionUID = -1628830250972241438L;

	/**
	 * Child Nodes.
	 */
	private LinkedList<TreeNode> children = new LinkedList<TreeNode>();

	/**
	 * The number of children for this node.
	 */
	private int numberOfChildren = 0;

	/**
	 * The Parent node.
	 */
	private TreeNode parent = null;

	/**
	 * The text for the node.
	 */
	private String text;

	/**
	 * The node type.
	 */
	private int type;

	/**
	 * Constructs a TreeNode with a token.  The text and state are stored from the token.
	 * @param text The text for the node.
	 * @param type The type of node.
	 */
	public DefaultTreeNode(final String text, final int type) {
		this.text = text;
		this.type = type;
	}

	/**
	 * Constructs a TreeNode with a token.  The text and state are stored from the token.
	 * @param token The token used to build the tree node.
	 */
	public DefaultTreeNode(final Token token) {
		this.text = token.getText();
		this.type = token.getState();
	}

	/**
	 * Constructs a TreeNode with a token.  The text and state are stored from the token.
	 * @param parent The parent of this node.
	 * @param text The text for the node.
	 * @param type The type of node.
	 */
	public DefaultTreeNode(final TreeNode parent, final String text, final int type) {
		this.parent = parent;
		this.text = text;
		this.type = type;
	}

	/**
	 * Constructs a TreeNode with a parent node and a token.  The text and state are stored from the token.
	 * @param parent The parent of this node.
	 * @param token The token used to build the tree node.
	 */
	public DefaultTreeNode(final TreeNode parent, final Token token) {
		this.parent = parent;
		this.text = token.getText();
		this.type = token.getState();
	}

	/**
	 * {@inheritDoc}
	 * @see kiff.util.model.TreeNode#addChild(kiff.util.model.TreeNode)
	 */
	public void addChild(final TreeNode node) {
		children.add(node);
		numberOfChildren++;
	}

	/**
	 * {@inheritDoc}
	 * @see kiff.util.model.TreeNode#addLeftChild(kiff.util.model.TreeNode)
	 */
	public void addLeftChild(final TreeNode node) {
		children.addFirst(node);
	}

	/**
	 * {@inheritDoc}
	 * @see kiff.util.model.TreeNode#addRightChild(kiff.util.model.TreeNode)
	 */
	public void addRightChild(final TreeNode node) {
		children.addLast(node);
	}

	/**
	 * {@inheritDoc}
	 * @see kiff.util.model.TreeNode#getChildren()
	 */
	public Collection<TreeNode> getChildren() {
		return children;
	}

	/**
	 * {@inheritDoc}
	 * @see kiff.util.model.TreeNode#getNumberOfChildren()
	 */
	public int getNumberOfChildren() {
		return numberOfChildren;
	}

	/**
	 * {@inheritDoc}
	 * @see kiff.util.model.TreeNode#getParent()
	 */
	public TreeNode getParent() {
		return parent;
	}

	/**
	 * {@inheritDoc}
	 * @see kiff.util.model.TreeNode#getText()
	 */
	public String getText() {
		return text;
	}

	/**
	 * {@inheritDoc}
	 * @see kiff.util.model.TreeNode#getType()
	 */
	public int getType() {
		return type;
	}

	/**
	 * {@inheritDoc}
	 * @see kiff.util.model.TreeNode#isLeaf()
	 */
	public boolean isLeaf() {
		return (numberOfChildren == 0);
	}

	/**
	 * {@inheritDoc}
	 * @see kiff.util.model.TreeNode#removeChildren()
	 */
	public void removeChildren() {
		children.clear();
	}

	/**
	 * {@inheritDoc}
	 * @see kiff.util.model.TreeNode#setParent(kiff.util.model.TreeNode)
	 */
	public void setParent(final TreeNode parent) {
		this.parent = parent;
	}

	/**
	 * {@inheritDoc}
	 * @see kiff.util.model.TreeNode#setText(java.lang.String)
	 */
	public void setText(final String text) {
		this.text = text;
	}

	/**
	 * {@inheritDoc}
	 * @see kiff.util.model.TreeNode#setType(int)
	 */
	public void setType(final int type) {
		this.type = type;
	}

	/**
	 * {@inheritDoc}
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return new ReflectionToStringBuilder(this)
			.toString();
	}
}
